/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NEHE.JOGL.Port;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.opengl.util.FPSAnimator;

public class JOGLTemplate extends JPanel implements GLEventListener {
   private static final int REFRESH_FPS = 60;    // Display refresh frames per second
   private GLU glu;             // For the GL Utility
   final FPSAnimator animator;  // Used to drive display() 
   
   // Constructor
   public JOGLTemplate() {
      GLCanvas canvas = new GLCanvas();
      this.setLayout(new BorderLayout());
      this.add(canvas, BorderLayout.CENTER);
      canvas.addGLEventListener(this);
   
      // Run the animation loop using the fixed-rate Frame-per-second animator,
      // which calls back display() at this fixed-rate (FPS).
      animator = new FPSAnimator(canvas, REFRESH_FPS, true);
   }
   
   // Main program
   public static void main(String[] args) {
      final int WINDOW_WIDTH = 640;
      final int WINDOW_HEIGHT = 480;
      final String WINDOW_TITLE = "JOGL Program Template";

      JFrame frame = new JFrame();
      final JOGLTemplate joglMain = new JOGLTemplate();
      frame.setContentPane(joglMain);
      frame.addWindowListener(new WindowAdapter() {
         @Override 
         public void windowClosing(WindowEvent e) {
            // Use a dedicate thread to run the stop() to ensure that the
            // animator stops before program exits.
            new Thread() {
               @Override 
               public void run() {
                  joglMain.animator.stop(); // stop the animator loop
                  System.exit(0);
               }
            }.start();
         }
      });
      frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
      frame.setVisible(true);
      frame.setTitle(WINDOW_TITLE);
      joglMain.animator.start(); // start the animation loop
   }
   
   // Implement methods defined in GLEventListener
   @Override
   public void init(GLAutoDrawable drawable) {
      // Your OpenGL codes to perform one-time initialization tasks 
      // such as setting up of lights and display lists.
   }
   
   @Override
   public void display(GLAutoDrawable drawable) {
      // Your OpenGL graphic rendering codes for each refresh.
   }
   
   @Override
   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
      // Your OpenGL codes to set up the view port, projection mode and view volume. 
   }

   @Override 
   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { 
      // Not implemented in JOGL.      
   }
}